var app = getApp(),
  a = app.requirejs("core");
Page({
  data: {
    tabs: ["手速榜", "总榜"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 15,
    page_s:1,
    page_r:1,
    loaded_s: false,
    speed_list: [],
    loaded_r: false,
    rank_list: [],
  
  },
  tabClick: function (a) {
    this.setData({
      sliderOffset: a.currentTarget.offsetLeft,
      activeIndex: a.currentTarget.id
    });
  },
  onLoad: function () {
    a.setting();
   this.rank();
    this.speed();
  },
  rank: function () {
    var that = this,
      page = that.data.page_r;
    a.get('Integral/allrank', { page: page }, function (t) {
      if (t.code == 0) {
        if (t.info.length > 0) {
          that.setData({
            rank_list: that.data.rank_list.concat(t.info),
            page_r: page + 1,
          })
          if(t.info.length<20){
            that.setData({ loaded_r: true})
          }
        }
        else {
          that.setData({
            loaded_r: true,
          })
        }

      } else {
        a.alert(t.msg);
      }
    }, !that.data.show)
  },
  speed: function () {
    var that = this,
      page = that.data.page_s;
    a.get('Integral/speedrank', {page:page}, function (t) {
      if (t.code == 0) {
        if (t.info.length > 0) {
          that.setData({
            speed_list: that.data.speed_list.concat(t.info),
            show: true,
            page_s: page + 1,
          })
          if (t.info.length < 20) {
            that.setData({ loaded_s: true })
          }
        }
        else {
          that.setData({
            loaded_s: true,
          })
        }

      } else {
        a.alert(t.msg);
      }
    },!that.data.show)
  },
  onPullDownRefresh: function () {
    var i = this.data.activeIndex;
    if(i==0){
      this.setData({
        speed_list:[],
        page_s:1,
        loaded_s:false
      });
      this.speed();
    }else if(i==1){
      this.setData({
        rank_list: [],
        page_r: 1,
        loaded_r: false
      });
      this.rank();
    }
   },
  onReachBottom: function () {
    var i = this.data.activeIndex;
    console.log(i+'上拉加载')
    if (i == 0) {
      this.loaded_s || this.speed();
    } else if (i == 1) {
      this.loaded_r || this.rank();
    }
  }
});